package com.ljl.module.system.rest;

import com.alibaba.fastjson.JSONObject;
import com.ljl.module.system.domain.ApiResponse;
import com.ljl.module.system.domain.Resource;
import com.ljl.module.system.service.ResourceService;
import com.ljl.module.system.util.ResourceState;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 20664
 */
@Slf4j
@RestController
@RequestMapping("/api/resource")
public class ResourceController {

    @Autowired
    private ResourceService resourceService;

    @GetMapping
    public ResponseEntity<ApiResponse<List<Resource>>> findAll() {
        List<Resource> all = resourceService.findAll();
        return ResponseEntity.ok(new ApiResponse<>(0, "操作成功", all));
    }

    @PostMapping
    public ResponseEntity<ApiResponse<Object>> save(@RequestBody Resource resource) {
        if (!StringUtils.hasText(resource.getUuid())) {
            return ResponseEntity.ok(new ApiResponse<>(400, "参数错误", "uuid不能为空"));
        }
        Resource res = resourceService.findByUuid(resource.getUuid());
        if (res != null) {
            return ResponseEntity.ok(new ApiResponse<>(400, "参数错误", "资源已存在"));
        }
        return ResponseEntity.ok(new ApiResponse<>(0, "操作成功", resourceService.save(resource)));
    }

    @PutMapping
    public ResponseEntity<ApiResponse<Object>> update(@RequestBody Resource resource) {
        if (!StringUtils.hasText(resource.getUuid())) {
            return ResponseEntity.ok(new ApiResponse<>(400, "参数错误", "uuid不能为空"));
        }
        Resource res = resourceService.findByUuid(resource.getUuid());
        if (res == null) {
            return ResponseEntity.ok(new ApiResponse<>(400, "参数错误", "资源不存在"));
        }
        if (res.getState() != ResourceState.NONE) {
            return ResponseEntity.ok(new ApiResponse<>(400, "参数错误", "请先暂停该资源的转换任务"));
        }
        return ResponseEntity.ok(new ApiResponse<>(0, "操作成功", resourceService.save(resource)));
    }

    @PutMapping("/merge")
    public ResponseEntity<ApiResponse<String>> save(@RequestBody JSONObject reqBody) {
        String uuid = reqBody.getString("uuid");
        if (!StringUtils.hasText(uuid)) {
            return ResponseEntity.ok(new ApiResponse<>(400, "参数错误", "uuid不能为空"));
        }
        Resource resource = resourceService.findByUuid(uuid);
        if (resource == null) {
            return ResponseEntity.ok(new ApiResponse<>(400, "参数错误", "找不到数据: " + uuid));
        }
        return ResponseEntity.ok(resourceService.merge(uuid));
    }

    @PostMapping("/analyze")
    public ResponseEntity<ApiResponse<List<Resource>>> analyzeResources(@RequestBody JSONObject reqBody) {
        String path = reqBody.getString("path");
        List<Resource> analyze;
        try {
            analyze = resourceService.analyze(path);
        } catch (Exception e) {
            log.error(e.getMessage());
            return ResponseEntity.ok(new ApiResponse<>(400, "操作失败", null));
        }
        log.info("解析资源: {}, 找到{}个资源", path, analyze.size());
        return ResponseEntity.ok(new ApiResponse<>(0, "操作成功", analyze));
    }
}
